package algotithm.leetcode.string.test395;

/**
 * @author zhouyanxiang
 * @Date 2021-01-2021/1/27-14:13
 */
public class Solution {

    public static void main(String[] args) {
        String s = "ababbc";
        int k = 2;
        Solution solution = new Solution();
        int substring = solution.longestSubstring(s, k);
        System.out.println(substring);
    }

    public int longestSubstring(String s, int k) {
        int[] str = new int[26];
        boolean[] flag = new boolean[26];
        for (int i = 0; i < s.length(); i++) {
            str[s.charAt(i) - 'a']++;
        }
        for (int i = 0; i < flag.length; i++) {
            if (str[i] < k) {
                flag[i] = true;
            }
        }

        boolean f = true;
        int start = 0;
        int res = 0;
        for (int i = 0; i < s.length(); i++) {
            if (flag[s.charAt(i) - 'a']) {
                f = false;
                if (start < i) {
                    res = Math.max(res,longestSubstring(s.substring(start,i),k));
                }
                start = i + 1;
            }
        }

        if (f) {
            return s.length();
        } else {
            if (start < s.length()) {
                res = Math.max(res,longestSubstring(s.substring(start,s.length()),k));
            }

        }
        return res;
    }

}
